package com.yan.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import org.apache.commons.collections.KeyValue;
import org.apache.commons.lang3.tuple.MutableTriple;
import org.apache.commons.lang3.tuple.Pair;


import java.util.*;

/**
 * Created by yanl_ on 2018/2/24.
 */
public class GuavaUtils {

    public static void main(String[] args) {

        ImmutableList<String> list = ImmutableList.of("a", "b", "c", "d");


        ImmutableMap<String, String> map = ImmutableMap.of("d", "aaa", "b", "bbb", "c", "ccc");


        int compare = Ints.compare(15, 10);


        List<Integer> lists = ImmutableList.of(1, 2, 3, 4);

        System.out.println(list);

        System.out.println(map);

        System.out.println(compare);

        System.out.println(lists);


        // 如果你想从字符串中得到所有的数字，那么你可以这样：
        String string1 = CharMatcher.DIGIT.retainFrom("some text 89983 and more");
        // 如果你想把字符串中的数据都去掉，可以如下：
        String string2 = CharMatcher.DIGIT.removeFrom("some text 89983 and more");

        System.out.println(string1 + "    string2=" + string2);


        int[] numbers = {1, 2, 3, 4, 5};

        String numbersAsString = Joiner.on(";").join(Ints.asList(numbers));

        System.out.println(numbersAsString);


        int[] array = {1, 2, 3, 4, 5, 10};
        boolean contains = Ints.contains(array, 10);
        System.out.println(contains);


        // 1元组
        List<Pair<String, Integer>> pairList = new ArrayList<Pair<String, Integer>>();

        pairList.add(Pair.of("One", 1));

        for (Pair p : pairList) {
            System.out.println(p.getLeft() + "    " + p.getRight());
        }


        List<MutableTriple<String, String, Integer>> mList = new ArrayList<MutableTriple<String, String, Integer>>();

        mList.add(MutableTriple.of("One", "OneMutable", 111));
        for (MutableTriple p : mList) {
            System.out.println(p.getLeft() + "    " + p.getMiddle() + "   " + p.getRight());
        }
        Pair p = Pair.of("left", "right");
        System.out.println("p.getLeft()     " + p.getLeft() + "    p.getRight() = " + p.getRight() + "  key=" + p.getKey() + "  value=" + p.getValue());

        MutableTriple m = MutableTriple.of("left1", "middle2", "right3");

        System.out.println("m.getLeft()+\"  \" + m.getMiddle() +\"  \" +  m.getRight() +\"  \" +  m.left +\"  \" +  m.middle +\"  \" +  m.right = " + m.getLeft() + "  " + m.getMiddle() + "  " + m.getRight() + "  " + m.left + "  " + m.middle + "  " + m.right);


        ImmutableMap<String, String> mapTest = ImmutableMap.of(
                "BNI", "{\"bankCode\":\"BNI\",\"sourceChannel\":\"Xendit\",\"organisationDes\":\"银行\",\"organisationType\":1}"

                , "PERMATA", "{\"bankCode\":\"PERMATA\",\"sourceChannel\":\"Doku\",\"organisationDesc\":\"银行\",\"organisationType\":1}");


        Map<String, String> mapJson = JSON.parseObject(mapTest.get("BNI").toString(), new TypeReference<Map<String, String>>() {
        }.getType());

        Map<String, String> mapObject = JSONObject.parseObject(mapTest.get("BNI"), Map.class);
        System.out.println(mapTest + "\n" + mapTest.get("BNI") + "\n" + mapJson + "\n" + mapObject);


        JSONObject obj1 = (JSONObject) JSONObject.toJSON(mapJson);
        JSONObject obj = (JSONObject) JSON.toJSON(mapJson);
        System.out.println(obj.get("bankCode") + " " + obj1.get("bankCode"));

        boolean flag = false;
        boolean flag1 = false;
        boolean flag2 = true;

        if ((flag || flag1 || flag2)) {
            System.out.println(true);
        }


        JSONObject jsonObject = new JSONObject();
        JSONObject paymentType = new JSONObject();
        paymentType.put("payment_type", "bank_transfer");

        JSONObject customerDetails = new JSONObject();
        customerDetails.put("email", "budi.utomo@Midtrans.com");
        customerDetails.put("first_name", "budi");
        customerDetails.put("last_name", "utomo");
        customerDetails.put("phone", "+6281 1234 1234");

        jsonObject.put("paymentType", paymentType);
        jsonObject.put("customerDetails", customerDetails);


        JSONObject itemDetails = new JSONObject();


        JSONArray subitemDetailsArray = new JSONArray();
        JSONObject subitemDetails = new JSONObject();


        subitemDetails.put("id", "1388998298204");
        subitemDetails.put("price", 5000);
        subitemDetails.put("quantity", 1);
        subitemDetails.put("name", "Ayam Zozozo");
        subitemDetailsArray.add(subitemDetails);

//        JSONObject subitemDetails1 = new JSONObject();

//        subitemDetails1.put("id", "1388998298204-X");
//        subitemDetails1.put("price", 50009);
//        subitemDetails1.put("quantity", 1);
//        subitemDetails1.put("name", "Ayam Zozozo-X");
//        subitemDetailsArray.add(subitemDetails1);


        jsonObject.put("item_details", subitemDetailsArray);


//        JSONObject transaction = new JSONObject();
//        transaction.put("order_id",orderId);
//        transaction.put("gross_amount",amount);
//        jsonObject.put("transaction_details",transaction);

        System.out.println(jsonObject);


        Map<String, Object> testMap = new HashMap<String, Object>();

        List<String> testListA = new ArrayList<String>();
        testListA.add("ZZZ");


        List<String> testListZ = new ArrayList<String>();
        testListZ.add("AAAA");

        List<String> testListF = new ArrayList<String>();
        testListF.add("FFFF");

        List<String> testListD = new ArrayList<String>();
        testListD.add("DDDD");

        testMap.put("A", testListA);
        testMap.put("D", testListD);
        testMap.put("Z", testListZ);
        testMap.put("F", testListF);

        testMap.put("B", new ArrayList<String>());
        System.out.println(testMap);

        for (Object listObj : testMap.values()) {
            List<String> arrayList = (List<String>) listObj;
            Collections.sort(arrayList);
        }


        System.out.println(testMap);


        ImmutableList<String> listA = ImmutableList.of("a", "b", "c", "d");
        Map<String, Object> stringListMap = new HashMap<String, Object>();
        for (String a : listA) {
            List<String> listobj = (List<String>) stringListMap.get(a);
            if (listobj == null) {
                listobj = new ArrayList<String>();
                stringListMap.put(a, listobj);
            }
            listobj.add(a);
        }

        
        System.out.println(stringListMap);
    }
}
